package com.portal.back.web;

/**
 * @Title:客户Action
 * @Copyright: Copyright (c) 0.1
 * @author:鲁根成
 * @version: 1.0
 * @time: 2011-4-13
 * 
 */
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.portal.back.service.ISysCustomService;
import com.portal.back.service.impl.SysCustomService;
import com.portal.base.web.BaseAction;

import com.portal.domain.SysCustom;
import com.portal.util.file.ImageSizer;
import com.portal.util.file.XmlUtil;
import com.portal.util.file.FileUtil;

@SuppressWarnings("serial")
public class SysCustomAction extends BaseAction {
	private ISysCustomService service;
	private SysCustom custom;
	private File imageFile;
	private String imageFileFileName;
	private String imageFileContentType;

	public SysCustomAction() {
		service = new SysCustomService();
	}

	/**
	 * 增加客户信息
	 * 
	 * @return
	 */
	public String addcustom() {
		String id = request.getParameter("id");
		if (imageFile != null) {
			String basePath = request.getSession().getServletContext()
					.getRealPath("/");
			String imagePatn = XmlUtil.getValue("imgPath", "sysConfig") + "/";
			String compressPath = XmlUtil.getValue("compression_diagram",
					"sysConfig")
					+ "/";
			String fileName = new Date().getTime() + imageFileFileName;
			if (FileUtil.doUpFile(basePath + imagePatn + fileName, imageFile)) {
				ImageSizer.doCompress(imageFile, basePath + compressPath
						+ fileName, 150, 150, 1.0f, true);
				custom.setLogoUrl(imagePatn + fileName);

			}

		}
		if (id == null || "".equals(id)) {
			boolean isFlag = service.savecustom(custom);
			if (isFlag)
				return "success";
			else
				return "error";
		} else {
			SysCustom old = service.getById(id);
			if (imageFile == null)
				custom.setLogoUrl(old.getLogoUrl());
			custom.setId(Long.parseLong(id));
			boolean isFlag = service.savecustom(custom);

			if (isFlag)
				return "success";
			else
				return "error";
		}

	}

	/**
	 * 得到所有客户信息
	 * 
	 * @return
	 */

	public String getAllcustom() {
		List<SysCustom> list = service.getAllCustom();
		ActionContext context = ActionContext.getContext();
		context.put("list", list);
		return "getAllCustom";
	}

	/**
	 * 删除客户信息
	 * 
	 * @return
	 */

	public String deletecustom() {
		String deleteByIdString = request.getParameter("id");
		boolean isFlag = service.deleteById(deleteByIdString);
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = null;
		if (isFlag)
			message = "删除成功";
		else
			message = "删除失败";
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");

		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String getcustomById() {
		String id = request.getParameter("id");
		SysCustom custom = service.getById(id);
		ActionContext context = ActionContext.getContext();
		context.put("custom", custom);
		return "somecustom";
	}

	public SysCustom getCustom() {
		return custom;
	}

	public void setCustom(SysCustom custom) {
		this.custom = custom;
	}

	public File getImageFile() {
		return imageFile;
	}

	public void setImageFile(File imageFile) {
		this.imageFile = imageFile;
	}

	public String getImageFileFileName() {
		return imageFileFileName;
	}

	public void setImageFileFileName(String imageFileFileName) {
		this.imageFileFileName = imageFileFileName;
	}

	public String getImageFileContentType() {
		return imageFileContentType;
	}

	public void setImageFileContentType(String imageFileContentType) {
		this.imageFileContentType = imageFileContentType;
	}

}
